﻿using MLSMooc.BO;
using MLSMooc.DAO.Interface.Menu;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using MLSMooc.Helper;
using System.Data.SqlClient;

namespace MLSMooc.DAO.Implement.Menu
{
   public class MenuDAO : IMenuDAO
    {
        private static MenuDAO instance = new MenuDAO();
        private MenuDAO() { }
        public static MenuDAO Getinstance() => instance;

        private static readonly string queryField = "select * from Mooc_Resource_Menu where isDelete=0";

        public async Task<IEnumerable<MenuBO>> GetMenuAsync()
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(@"select mr.* from Mooc_Resource_Menu as mr
  left join Mooc_Course as mc on mr.ResourceId = mc.Id and mr.Type=2 and mc.isDelete=0
  left join Mooc_Project mp on mp.Id=mr.ResourceId and mr.Type=1 and mp.isDelete=0
  where mr.isDelete = 0 order by  mp.Sort, mc.Sort");
                var res = await conn.QueryAsync<MenuBO>(sql.ToString());
                return res;
            }
        }

        public async Task<IEnumerable<MenuBO>> GetMenuResourceList()
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder();
                sql.Append($@"select m.*,p.Pro_Books,p.Pro_Textbooks,p.Pro_Websites from Mooc_Resource_Menu m join Mooc_Project p  on m.ResourceId=p.Id where m.Type=1 and m.Mid=0 and m.isDelete=0");
                var res = await conn.QueryAsync<MenuBO>(sql.ToString());
                return res;
            }
        }
    }
}
